package com.hqyj.lls.service.impl;

import com.hqyj.lls.entity.Account;
import com.hqyj.lls.mapper.AccountMapper;
import com.hqyj.lls.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
//    @Transactional //交由spring管理当前方法事务！ spring处理事务必须建立在发生异常时才会rollback
    public boolean changeMoney() {
        //a-100
        //先查出转账人为张三的数据
        Account zhangsan = accountMapper.selectByAccountName("zhangsan");
        //张三的钱-100，再更新数据库
        zhangsan.setMoney(zhangsan.getMoney()-100);
        accountMapper.updateAccount(zhangsan);



        Account lisi = accountMapper.selectByAccountName("lisi");
        //李四的钱+100，再更新数据库
        lisi.setMoney(lisi.getMoney()+100);
        accountMapper.updateAccount(lisi);
        return true;
    }
}
